package br.com.felix.easyclearcase.ui.dialogs;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import br.com.felix.clearcase.command.ParametrizedCommand;
import br.com.felix.easyclearcase.EasyClearCaseExplorerConfiguration;

public class RemoveCommandDialog extends JDialog {


	private static final long serialVersionUID = 6997079577630386567L;

	private JList jlstCommands = null;
	private JScrollPane jsp = new JScrollPane();
	
	public RemoveCommandDialog(){
		setTitle("Remove command");
		setLayout(new GridBagLayout());
		setSize(new Dimension(400,400));
		 
		populateCommandList();	
		
		JButton btnRemove = new JButton("Remove");
		btnRemove.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				removeCommand();
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				dispose();
			}
		});
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.EAST;
		gbc.gridwidth = 1;
		gbc.gridy = 1;
		gbc.gridx = 1;
		add(btnRemove,gbc);
		gbc.gridx = 2;
		add(btnCancel,gbc);
			
	}


	private void populateCommandList() {
		if(jlstCommands!=null){
			remove(jsp);
		}
		EasyClearCaseExplorerConfiguration eccec = EasyClearCaseExplorerConfiguration.instance();
		List<ParametrizedCommand> lstCommands = eccec.getParametrizedCommandsList();
		jlstCommands = new JList(lstCommands.toArray());

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.EAST;
		gbc.gridwidth = 4;
		gbc.gridx = 0;
		gbc.gridy = 0;
		jsp = new JScrollPane(jlstCommands);
		jsp.setPreferredSize(new Dimension(300,200));
		add(jsp,gbc);
		validate();
	}

	
	protected void removeCommand() {
		int index = jlstCommands.getSelectedIndex();
		if(index < 0){
			JOptionPane.showMessageDialog(getParent(), "Please select a command to remove!","Warning",JOptionPane.WARNING_MESSAGE);
			return;
		}
		Object command = jlstCommands.getSelectedValue();
		ParametrizedCommand pc = (ParametrizedCommand)command;
		EasyClearCaseExplorerConfiguration eccec = EasyClearCaseExplorerConfiguration.instance();
		eccec.removeCommand(pc);

		populateCommandList();
		
		JOptionPane.showMessageDialog(getParent(), "Command removed. Please re-start the application for changes to take effect.","Information message",JOptionPane.INFORMATION_MESSAGE);
	}
	
	
	public static void main(String args[]){
		RemoveCommandDialog rcd = new RemoveCommandDialog();
		rcd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		rcd.setVisible(true);
	}
	
}
